package learn.generics;

import java.util.ArrayList;
import java.util.Random;

public class product_getter<T>{
    public static void main(String[] args) {
        //创建抽奖其对象，指定数据类型
        product_getter<String> stringproduct_getter=new product_getter<>();
        //给抽奖其填充商品
        String[] srtProduct={"苹果手机" ,
                "华为手机" ,
                "扫地机器人" ,
                "咖啡机"};
        for (int i=0;i<srtProduct.length;i++){
            stringproduct_getter.addProduct(srtProduct[i]);
        }
        //抽奖
        String product1=stringproduct_getter.getProduct();
        System.out.println("恭喜您抽中了: "+product1);
        System.out.println("-----------------------------------------");
        product_getter<Integer> integerproduct_getter=new product_getter<>();
        int[] intProducts={10000,5000,300000,10};
        for (int i=0;i<intProducts.length;i++){
            integerproduct_getter.addProduct(intProducts[i]);

        }
        Integer products2=integerproduct_getter.getProduct();
//        Integer product = integerproduct_getter.getProduct();
        System.out.println("恭喜您抽中了: "+products2);
    }
    Random random=new Random();
    //奖品
    private T product;
    //奖品池
    ArrayList<T> list=new ArrayList<>();
    //添加奖品
    public void addProduct(T t){
        list.add(t);
    }

//抽奖

    /**
     *
     * @return
     */
    public T getProduct() {
        product=list.get(random.nextInt(list.size()));
        return product;
    }
}
